package com.oom;

import java.text.SimpleDateFormat;
import java.util.concurrent.*;

/**
 * @Author: tgy
 * @Date: 2020-09-03 17:42
 */
public class ThreadLocalDemo01 {

    public static  final InheritableThreadLocal<String> inheritableThreadLocal = new InheritableThreadLocal<>();
    public static final ThreadLocal<String> threadLocal = new ThreadLocal<>();


    public static final ThreadLocal<Byte[]> threadLocalByte = new ThreadLocal<Byte[]>(){

        @Override
        protected Byte[] initialValue() {

            // 1M
            return new Byte[1024*1024];
        }
    };

    public static void main(String[] args) {


        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 15, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(30));


        for (int j = 0; j < Integer.MAX_VALUE; j++) {

            for (int i = 0; i < 100; i++) {

                poolExecutor.execute(()->{

                    threadLocalByte.get();

                });

            }

        }



    }

    private static void test01() {
        //
//        ThreadLocal<SimpleDateFormat> stringThreadLocal = new ThreadLocal<SimpleDateFormat>(){
//
//            @Override
//            protected SimpleDateFormat initialValue() {
//                return new SimpleDateFormat();
//            }
//        };


//        stringThreadLocal.get();

//        Thread thread = Thread.currentThread();

//        ExecutorService executorService = Executors.newFixedThreadPool(10);

//        executorService.invokeAll()

        inheritableThreadLocal.set("Inheritable hello");
        threadLocal.set("hello");
        new Thread(()->{
            System.out.println(String.format("子线程可继承值：%s",inheritableThreadLocal.get()));
            System.out.println(String.format("子线程值：%s",threadLocal.get()));
            new Thread(()->{
                System.out.println(String.format("孙子线程可继承值：%s",inheritableThreadLocal.get()));
                System.out.println(String.format("孙子线程值：%s",threadLocal.get()));
            },"1234").start();

        },"abc").start();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
    }
}
